package com.ariel;


import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

//ver como usarlo
//import android.media.AudioFormat;

public class GameView extends SurfaceView {
    private SurfaceHolder holder;
    private GameLoopThread gameLoopThread;
    private List<Sprite> sprites = new ArrayList<Sprite>();
	private long lastClick;
	private Bitmap bmpBlood;
	private List<TempSprite> temps = new ArrayList<TempSprite>();
//	private SoundPool sounds;
//	private int sonido_aux;
   
    public GameView(Context context) {
          super(context);
          gameLoopThread = new GameLoopThread(this);
          holder = getHolder();
          holder.addCallback(new Callback() {

                 public void surfaceDestroyed(SurfaceHolder holder) {
                        boolean retry = true;
                        gameLoopThread.setRunning(false);
                        while (retry) {
                               try {
                                     gameLoopThread.join();
                                     retry = false;
                               } catch (InterruptedException e) {
                               }
                        }
                 }

                 public void surfaceCreated(SurfaceHolder holder) {
                     //definicion del objeto para la generacion de sonidos
//                     sonido_aux = sounds.load(getContext(), R.raw.pone_ficha, 0);
//                     sonidos = new Sonidos(context);
                	 	createSprites();
                        gameLoopThread.setRunning(true);
                        gameLoopThread.start();
                 }

                 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
                 }
          });
          bmpBlood = BitmapFactory.decodeResource(getResources(), R.drawable.blood1);
    }
    
    private void createSprites() {
        sprites.add(createSprite(R.drawable.bad1));
        sprites.add(createSprite(R.drawable.bad2));
        sprites.add(createSprite(R.drawable.bad3));
        sprites.add(createSprite(R.drawable.bad4));
        sprites.add(createSprite(R.drawable.bad5));
        sprites.add(createSprite(R.drawable.bad6));
        sprites.add(createSprite(R.drawable.good1));
        sprites.add(createSprite(R.drawable.good2));
        sprites.add(createSprite(R.drawable.good3));
        sprites.add(createSprite(R.drawable.good4));
        sprites.add(createSprite(R.drawable.good5));
        sprites.add(createSprite(R.drawable.good6));
    }
    
    private Sprite createSprite(int resouce) {
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), resouce);
        return new Sprite(this,bmp);
  }
    @Override
    protected void onDraw(Canvas canvas) {
          canvas.drawColor(Color.BLACK);
          //lo ponemos aca como para que queden de fondo
          for (int i = temps.size() - 1; i >= 0; i--) {
                temps.get(i).onDraw(canvas);
          }
          for (Sprite sprite : sprites) {
              sprite.onDraw(canvas);
          }
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (System.currentTimeMillis() - lastClick > 300) {
            lastClick = System.currentTimeMillis();
	        synchronized (getHolder()) {
	        	float x = event.getX();
	        	float y = event.getY();
	            for (int i = sprites.size()-1; i >= 0; i--) {
	                   Sprite sprite = sprites.get(i);
	                   if (sprite.isCollition(x,y)) {
//	                	   sounds.play(sonido_aux, 0.5f, 0.5f, 2, 0, 1);
//	                	   sonidos.poneFicha();
                          sprites.remove(sprite);
                          temps.add(new TempSprite(temps, this, x, y, bmpBlood));
                          break;
	                   }
	            }
	        }
        }
          return true;
//          para que no se siga evaluando posteriores listeners
//          return super.onTouchEvent(event);
    }
}